package cn.hy.dataSync.utils;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;


public class MD5Utils {

    public static String encode(String src, Charset charset) {
        if (src == null || src.length() == 0) {
            return null;
        }
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            byte[] srcBytes = messageDigest.digest(src.getBytes(charset));
            StringBuilder stringBuffer = new StringBuilder(32);
            for (byte b : srcBytes) {
                int val = b & 0xff;
                if (val < 0x10) {
                    stringBuffer.append("0");
                }
                stringBuffer.append(Integer.toHexString(val));
            }
            return stringBuffer.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String encode(String src) {
        return encode(src, StandardCharsets.UTF_8);
    }
}
